package com.gray.blog.service.front;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.gray.blog.common.enums.DelFlagTypeEnum;
import com.gray.blog.common.enums.ErrorCodeEnum;
import com.gray.blog.common.enums.YesOrNoEnum;
import com.gray.blog.common.result.ModelResult;
import com.gray.blog.common.util.BeanUtil;
import com.gray.blog.common.util.PagerUtil;
import com.gray.blog.common.util.ResultUtil;
import com.gray.blog.common.util.StringUtil;
import com.gray.blog.dao.front.FrontGeeksDao;
import com.gray.blog.entity.front.FrontGeeksEntity;
import com.gray.blog.entity.jsonEntity.FrontGeeksExtraInfo;
import com.gray.blog.result.sys.GeeksDetailResult;
import com.gray.blog.result.sys.GetPageGeeksResult;
import com.gray.blog.service.BaseService;
import com.gray.blog.vo.sys.AddSysGeeksVO;
import com.gray.blog.vo.sys.GetPageGeeksVO;
import com.gray.blog.vo.sys.UpdateGeeksInfoVO;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created  By zhoux 2020/04/24
 **/
@Service
@Slf4j
public class FrontGeeksService extends BaseService<FrontGeeksDao, FrontGeeksEntity> {

    @Autowired
    private FrontGeeksDao frontGeeksDao;

    public ModelResult<GetPageGeeksResult> getPageGeeks(GetPageGeeksVO vo) {
        IPage<FrontGeeksEntity> frontGeeksEntityIPage = super.findItemByPage(vo.getPageNum(), vo.getPageSize(), vo.getKeyWordType(), vo.getKeyWord());
        PagerUtil<GetPageGeeksResult> pagerUtil = new PagerUtil<>();
        GetPageGeeksResult getPageGeeksResult = pagerUtil.setPageResultByPageObject(frontGeeksEntityIPage, new GetPageGeeksResult(), GeeksDetailResult.class);
        pagerUtil.setPageResultTime(getPageGeeksResult.getDataList());
        return ModelResult.newSuccess(getPageGeeksResult);
    }

    public ModelResult<GeeksDetailResult> getSysGame(String id) {
        FrontGeeksEntity frontGeeksEntity = frontGeeksDao.getById(id);
        if (frontGeeksEntity == null) {
            log.warn("FrontGeeksService.getSysGame frontGeeksEntity is null id:{}", id);
            return new ModelResult<>(ErrorCodeEnum.GEEK_INFO_NOT_EXIST);
        }
        GeeksDetailResult geeksDetailResult = BeanUtil.copy(frontGeeksEntity, GeeksDetailResult.class);
        ResultUtil.buildResultTime(geeksDetailResult);
        return ModelResult.newSuccess(geeksDetailResult);
    }

    public ModelResult addSysGeeks(AddSysGeeksVO vo) {
        FrontGeeksEntity entity = new FrontGeeksEntity();
        entity.setExtraInformation(new FrontGeeksExtraInfo(vo.getTitle(), vo.getIntroduction(), YesOrNoEnum.NO.getType(), vo.getCover()));
        entity.setContent(vo.getContent());
        entity.setId(StringUtil.getUUID());
        entity.setCreateBy(vo.getUserId());
        entity.setUpdateBy(vo.getUserId());
        entity.setDelFlag(DelFlagTypeEnum.NOT_DELETE.getType());
        frontGeeksDao.insert(entity);
        return ModelResult.newSuccess();
    }

    public ModelResult updateSysGeeks(UpdateGeeksInfoVO vo) {
        FrontGeeksEntity entity = new FrontGeeksEntity();
        entity.setId(vo.getId());
        entity.setExtraInformation(new FrontGeeksExtraInfo(vo.getTitle(), vo.getIntroduction(), YesOrNoEnum.NO.getType(), vo.getCover()));
        entity.setContent(vo.getContent());
        int updateResult = frontGeeksDao.update(entity);
        if (updateResult != 1) {
            log.warn("FrontGeeksService.updateSysGeeks error vo:{}", vo);
            return new ModelResult<>(ErrorCodeEnum.SYSTEM_ERROR);
        }
        return ModelResult.newSuccess();
    }

    public ModelResult<List<GeeksDetailResult>> getFrontGeeksInfo() {
        List<GeeksDetailResult> geeksDetailResultList = Lists.newArrayList();
        Map<String, Object> param = Maps.newHashMap();
        param.put("showInFront", YesOrNoEnum.YES.getType());


    }
}
